package com.vt.auth.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.vt.common.security.handler.MobileLoginSuccessHandler;
import com.vt.common.security.mobile.MobileSecurityConfigurer;
import com.vt.common.security.service.CustomUserDetailsService;
import com.vt.common.security.social.SocialSecurityConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

/**
 * @Auther: PanZha
 * @Date: 2018/10/11 12:26
 * 认证相关配置
 */
@Primary
@Order(90)
@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {

    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private ClientDetailsService clientDetailsService;
    @Autowired
    private CustomUserDetailsService userDetailsService;
    @Lazy
    @Autowired
    private AuthorizationServerTokenServices defaultAuthorizationServerTokenServices;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers(
                        "/actuator/**",
                        "/oauth/removeToken",
                        "/oauth/delToken/*",
                        "/oauth/listToken",
                        "/mobile/**",
                        "/social/**",
                        "/admin/user/login_sms").permitAll()
                .anyRequest().authenticated()
                .and().csrf().disable()
                .apply(mobileSecurityConfigurer())
                .and()
                .apply(socialSecurityConfigurer());
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public MobileSecurityConfigurer mobileSecurityConfigurer() {
        MobileSecurityConfigurer mobileSecurityConfigurer = new MobileSecurityConfigurer();
        mobileSecurityConfigurer.setMobileLoginSuccessHandler(mobileLoginSuccessHandler());
        mobileSecurityConfigurer.setUserDetailsService(userDetailsService);
        return mobileSecurityConfigurer;
    }

    @Bean
    public AuthenticationSuccessHandler mobileLoginSuccessHandler() {
        return MobileLoginSuccessHandler.builder()
                .objectMapper(objectMapper)
                .clientDetailsService(clientDetailsService)
                .passwordEncoder(passwordEncoder())
                .defaultAuthorizationServerTokenServices(defaultAuthorizationServerTokenServices).build();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }


    @Bean
    public SocialSecurityConfigurer socialSecurityConfigurer() {
        SocialSecurityConfigurer socialSecurityConfigurer = new SocialSecurityConfigurer();

        socialSecurityConfigurer.setMobileLoginSuccessHandler(mobileLoginSuccessHandler());
        socialSecurityConfigurer.setUserDetailsService(userDetailsService);
        return socialSecurityConfigurer;
    }
}
